package org.jeecg.modules.procurement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.NumberUtil;
import org.jeecg.modules.procurement.entity.BussMaterialProcurementDetail;
import org.jeecg.modules.procurement.mapper.BussMaterialProcurementDetailMapper;
import org.jeecg.modules.procurement.service.IBussMaterialProcurementDetailService;
import org.jeecg.modules.procurement.vo.BussMaterialProcurementDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 原料采购订单明细
 * @Author: jeecg-boot
 * @Date:   2024-06-06
 * @Version: V1.0
 */
@Service
public class BussMaterialProcurementDetailServiceImpl extends ServiceImpl<BussMaterialProcurementDetailMapper, BussMaterialProcurementDetail> implements IBussMaterialProcurementDetailService {
	
	@Autowired
	private BussMaterialProcurementDetailMapper bussMaterialProcurementDetailMapper;
	
	@Override
	public List<BussMaterialProcurementDetailVo> selectByMainId(String mainId) {
		List<BussMaterialProcurementDetailVo> bussMaterialProcurementDetailVos = bussMaterialProcurementDetailMapper.selectByMainId(mainId);
		bussMaterialProcurementDetailVos.stream().forEach(item->{
			BigDecimal planNum = item.getPlanNum();
			if(planNum != null){
				BigDecimal sum = bussMaterialProcurementDetailMapper.sumOrder(item.getProcurementPlanId());
				item.setGeneratedNum(sum);
				item.setRemainderNum(planNum.subtract(sum));
			}
		});
		return bussMaterialProcurementDetailVos;
	}

	@Override
	public IPage<BussMaterialProcurementDetailVo> pageList(Page<BussMaterialProcurementDetailVo> page, QueryWrapper<BussMaterialProcurementDetailVo> queryWrapper) {
		return bussMaterialProcurementDetailMapper.pageList(page,queryWrapper);
	}

	/**
	 * 计算订单数量，剩余数量
	 * @param vo
	 */
	@Override
	public BussMaterialProcurementDetailVo computeQuantity(BussMaterialProcurementDetailVo vo){
		BigDecimal planNum = vo.getPlanNum();
		BigDecimal sum = bussMaterialProcurementDetailMapper.sumOrder(vo.getProcurementPlanId());
		if(vo.getId() == null){
			BigDecimal procurementNum = vo.getProcurementNum();
			BigDecimal subtract = planNum.subtract(sum.add(NumberUtil.getBigDecimal(procurementNum,0)));
			vo.setRemainderNum(subtract);
			vo.setGeneratedNum(sum.add(NumberUtil.getBigDecimal(procurementNum,0)));
			return vo;
		}
		BussMaterialProcurementDetail detail = this.getById(vo.getId());
		BigDecimal procurementNum = vo.getProcurementNum();
		BigDecimal subtract = planNum.subtract(sum.add(NumberUtil.getBigDecimal(procurementNum,0))
				.subtract(NumberUtil.getBigDecimal(detail.getProcurementNum(),0)));
		vo.setRemainderNum(subtract);
		vo.setGeneratedNum(sum.add(procurementNum).subtract(NumberUtil.getBigDecimal(detail.getProcurementNum(),0)));
		return vo;
	}
}
